home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWResour / Sources / FWCFMRes.cpp next >
Encoding:
Text File  |  1995-11-08  |  6.3 KB  |  220 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWCFMRes.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWCFMRES_H
  13. #include "FWCFMRes.h"
  14. #endif
  15.  
  16. #ifndef FWSTDDEF_H
  17. #include "FWStdDef.h"
  18. #endif
  19.  
  20. #ifndef FWEXCLIB_H
  21. #include "FWExcLib.h"
  22. #endif
  23.  
  24. #ifndef FWRESOUR_H
  25. #include "FWResour.h"
  26. #endif
  27.  
  28. #ifndef FWFILES_H
  29. #include "FWFiles.h"
  30. #endif
  31.  
  32. #ifdef FW_BUILD_MAC
  33.  
  34.     #ifndef __ERRORS__
  35.     #include <Errors.h>
  36.     #endif
  37.     
  38.     #ifndef __RESOURCES__
  39.     #include <Resources.h>
  40.     #endif
  41.     
  42.     #ifndef __CODEFRAGMENTS__
  43.     #include <CodeFragments.h>
  44.     #endif
  45.  
  46.     #if defined(SYMANTEC_CPLUS) && !FW_LIB_EXPORT_PRAGMAS
  47.     #pragma internal on
  48.     #endif
  49.  
  50. #endif
  51.  
  52. //========================================================================================
  53. //    Global Variable
  54. //========================================================================================
  55. //    FW_gInstance is initialized in InitLibraryResources on the Mac
  56. //    FW_gInstance is defined in FWLbInit.cpp for Windows
  57. #ifdef FW_BUILD_MAC
  58. FW_Instance FW_gInstance = NULL;
  59. #endif
  60.  
  61. //========================================================================================
  62. //    Global functions
  63. //========================================================================================
  64.  
  65. #ifdef FW_BUILD_MAC
  66. //----------------------------------------------------------------------------------------
  67. // InitLibraryResources
  68. //----------------------------------------------------------------------------------------
  69.  
  70. OSErr InitLibraryResources(CFragInitBlockPtr init)
  71. {
  72.     switch( init->fragLocator.where )
  73.     {
  74.         case kDataForkCFragLocator:
  75.         case kResourceCFragLocator:
  76.             FW_gInstance = (FW_Instance) NewPtr(sizeof(FSSpec));
  77.             if( MemError() )
  78.                 return MemError();
  79.             *FW_gInstance = *init->fragLocator.u.onDisk.fileSpec;
  80.             break;
  81.             
  82.         case kMemoryCFragLocator:
  83.             // An in-memory fragment does not have any resource fork.
  84.             // Any future calls to Begin... will fail (see below)
  85.             break;
  86.             
  87.         default:
  88.             ::DebugStr((unsigned char*) "\pUnknown segment location!");
  89.             break;
  90.     }
  91.     return noErr;
  92. }
  93.  
  94. #endif
  95.  
  96. //========================================================================================
  97. //    class FW_CAcquireCFMResourceAccess
  98. //========================================================================================
  99.  
  100. #ifdef FW_BUILD_MAC
  101. short FW_CAcquireCFMResourceAccess::gLibraryRefCount;        // C++ gauranteed to be 0
  102. short FW_CAcquireCFMResourceAccess::gLibraryRefNum = -1;
  103. #endif
  104.  
  105. //----------------------------------------------------------------------------------------
  106. // FW_CAcquireCFMResourceAccess::FW_CAcquireCFMResourceAccess
  107. //----------------------------------------------------------------------------------------
  108.  
  109. FW_CAcquireCFMResourceAccess::FW_CAcquireCFMResourceAccess()
  110. {
  111. #ifdef FW_BUILD_MAC
  112.     CommonInit(FW_gInstance);
  113. #endif
  114. }
  115.  
  116. //----------------------------------------------------------------------------------------
  117. // FW_CAcquireCFMResourceAccess::FW_CAcquireCFMResourceAccess
  118. //----------------------------------------------------------------------------------------
  119.  
  120. FW_CAcquireCFMResourceAccess::FW_CAcquireCFMResourceAccess(FW_Instance instance)
  121. {
  122. #ifdef FW_BUILD_MAC
  123.     CommonInit(instance);
  124. #endif
  125. }
  126.  
  127. #ifdef FW_BUILD_MAC
  128. //----------------------------------------------------------------------------------------
  129. // FW_CAcquireCFMResourceAccess::CommonInit
  130. //----------------------------------------------------------------------------------------
  131.  
  132. void FW_CAcquireCFMResourceAccess::CommonInit(FW_Instance instance)
  133. {
  134.     FW_ASSERT(instance != NULL);
  135.     
  136.     fSavedLibraryRefNum = ::CurResFile();
  137.     
  138.     if (gLibraryRefCount == 0)
  139.     {
  140.         gLibraryRefNum = ::FSpOpenResFile(instance, fsRdPerm);
  141.         FW_FailOnError(::ResError());
  142.     }
  143.     
  144.     ::UseResFile(gLibraryRefNum);
  145.     FW_FailOnError(::ResError());
  146.     gLibraryRefCount++;
  147. }
  148. #endif
  149.  
  150. //----------------------------------------------------------------------------------------
  151. // FW_CAcquireCFMResourceAccess::~FW_CAcquireCFMResourceAccess
  152. //----------------------------------------------------------------------------------------
  153.  
  154. FW_CAcquireCFMResourceAccess::~FW_CAcquireCFMResourceAccess()
  155. {
  156. #ifdef FW_BUILD_MAC
  157.     if(--gLibraryRefCount == 0)
  158.     {
  159.         ::CloseResFile(gLibraryRefNum);
  160.         gLibraryRefNum = -1;
  161.     }
  162.     
  163.     ::UseResFile(fSavedLibraryRefNum);
  164. #endif
  165. }
  166.  
  167. //========================================================================================
  168. //    class FW_CSharedLibraryResourceFile
  169. //========================================================================================
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // FW_CSharedLibraryResourceFile::FW_CSharedLibraryResourceFile
  173. //----------------------------------------------------------------------------------------
  174.  
  175. FW_CSharedLibraryResourceFile::FW_CSharedLibraryResourceFile():
  176.     FW_CResourceFile()
  177. #ifdef FW_BUILD_MAC
  178.     ,fAcquireCFMResourceAccess(FW_gInstance)
  179. #endif
  180. {
  181.     CommonInit(FW_gInstance);
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. // FW_CSharedLibraryResourceFile::FW_CSharedLibraryResourceFile
  186. //----------------------------------------------------------------------------------------
  187.  
  188. FW_CSharedLibraryResourceFile::FW_CSharedLibraryResourceFile(FW_Instance instance):
  189.     FW_CResourceFile()
  190. #ifdef FW_BUILD_MAC
  191.     ,fAcquireCFMResourceAccess(instance)
  192. #endif
  193. {
  194.     CommonInit(instance);
  195. }
  196.  
  197. //----------------------------------------------------------------------------------------
  198. // FW_CSharedLibraryResourceFile::CommonInit
  199. //----------------------------------------------------------------------------------------
  200.  
  201. void FW_CSharedLibraryResourceFile::CommonInit(FW_Instance instance)
  202. {    
  203. #ifdef FW_BUILD_MAC
  204.     FW_PPrivResourceFile privResourceFile(new FW_CPrivResourceFileRep(fAcquireCFMResourceAccess.GetLibraryRefNum()));
  205. #endif
  206. #ifdef FW_BUILD_WIN
  207.     FW_PPrivResourceFile privResourceFile(new FW_CPrivResourceFileRep(instance));
  208. #endif
  209.  
  210.     SetRep(privResourceFile);
  211. }
  212.  
  213. //----------------------------------------------------------------------------------------
  214. // FW_CSharedLibraryResourceFile::~FW_CSharedLibraryResourceFile
  215. //----------------------------------------------------------------------------------------
  216.  
  217. FW_CSharedLibraryResourceFile::~FW_CSharedLibraryResourceFile()
  218. {
  219. }
  220.